<template>
  <div>
    <el-row type="flex" justify="center">
      <el-col :span="6">
        <el-form label-width="80px">
          <el-form-item label="商品名">
            <el-input v-model="name" placeholder="请输入商品名"></el-input>
          </el-form-item>
          <el-form-item label="价格">
            <el-input v-model="price" placeholder="请输入价格">单位是分</el-input>
          </el-form-item>
          <el-form-item label="简介">
            <el-input v-model="desc" placeholder="请输入简介">单位是分</el-input>
          </el-form-item>
          <el-form-item label="首图">
            <el-input type="file"></el-input>
          </el-form-item>
          <el-form-item label="所属分类1">
            <el-select v-model="cate1">
              <el-option :value="item.id" :label="item.name" v-for="item in cate_list1" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属分类2">
            <el-select v-model="cate2">
              <el-option :value="item.id" :label="item.name" v-for="item in cate_list2" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属分类3">
            <el-select v-model="cate3">
              <el-option :value="item.id" :label="item.name" v-for="item in cate_list3" :key="item.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="所属标签">
            <el-select v-model="tag">
              <el-option :value="item.id" :label="item.name" v-for="item in tag_list" :key="item.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="商品规格">
            
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="add">添加</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
    data(){
        return {
            name:"华为手机P30",
            price:"8888",
            desc:"好手机我的最爱",
            cate1:"",
            cate2:"",
            cate3:"",
            tag:"",
            params:"",
            cate_list1:[],
            cate_list2:[],
            cate_list3:[],
            tag_list:[],

        }
    },
    created(){
        //访问一级分类 
        this.axios.get("/cate").then(resp=>{
            console.log(resp.data)
            this.cate_list1=resp.data
        }).catch(error=>{
            console.log(error)
        })
    },
    watch:{
      cate1(val1,val2){
        this.axios.get("cate?parent_id="+val1).then(resp=>{
          console.log(resp.data)
          this.cate_list2=resp.data
        })

        this.axios.get("/tag?cate="+this.cate1).then(resp=>{
            console.log(resp.data)
            this.tag_list=resp.data
        }).catch(error=>{
            console.log(error)
        })

      },
      cate2(val1,val2){
        this.axios.get("cate?parent_id="+val1).then(resp=>{
          console.log(resp.data)
          this.cate_list3=resp.data
        })
      }
    },
    methods:{
        add(){
            const data={
                name:this.name,
                price:this.price,
                desc:this.desc,
                cate1:this.cate1,
                cate2:this.cate2,
                cate3:this.cate3,
                tag:this.tag,
            }
            this.axios.post("/goods",data).then(resp=>{
                console.log(resp.data)
                this.$message({
                    "message":"添加商品成功",
                    "type":"success",
                })
            }).catch(error=>{
                console.log(error)
                this.$message.error("添加商品失败")
            })
        }
    }
};
</script>

<style>
</style>